package com.example.kafka.search.impl;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.rest.RestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * 指标数据管理
 *
 * @author dean.Yang
 * @date 2022-02-22 13:51
 */
@Service
public class IDInfoManageService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void createIndexOne(TransportClient client) {
        // 创建索引
        try {
            IndexResponse response = client.prepareIndex("twitter", "_doc", "1")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("user", "kimchy")
                            .field("postDate", new Date())
                            .field("message", "trying out Elasticsearch")
                            .endObject()).get();
            // 获取索引
            String _index = response.getIndex();
            // 获取类型
            String _type = response.getType();
            // 获取ID
            String _id = response.getId();
            // 获取版本号
            long _version = response.getVersion();
            // 执行语句的运行结果状态
            RestStatus status = response.status();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public void sendIndex(Object o){
//        Map<String, Object> jsonMap1 = new HashMap<>();
//        //指标值
//        jsonMap1.put("endpoint", "aiops1");
//        //指标中文解释
//        jsonMap1.put("metric", "cpu.idle");
//        IndexRequest indexRequest1 = new IndexRequest("endpoint_relate_index").id("172.28.202.136-df.used").source(jsonMap1);
//        // 同步执行
//        try {
//            IndexResponse indexResponse1 = restHighLevelClient.index(indexRequest1, RequestOptions.DEFAULT);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        Map<String, Object> jsonMap = new HashMap<>();
//        //指标中文解释
//        jsonMap.put("endpoint", "aiops1");
//        //指标值
//        jsonMap.put("value", "rtdb_server PUBLIC -scn realtime -scn_inst 1 -subscn_inst 1 -subscn public");
//        //数据采集的间隔
//        jsonMap.put("step", 40);
//        //时间戳，`date +%s`，自UTC时间1970-01-01 00:00:00到当前时间的秒数
//        jsonMap.put("timestamp", "1646015058");
//        //指标ID
//        jsonMap.put("indexId", 40);
//        IndexRequest indexRequest = new IndexRequest("indexdata").id("172.28.202.136-df.used").source(jsonMap);
//        // 同步执行
//        try {
//            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
